package com.ziytek.web.citizen.controller;

import com.ziytek.web.citizen.constant.ApiConstant;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * 服务关闭及重启接口
 *
 * @className ServiceShutdownAndReStartController
 * @description: 服务关闭及重启接口
 * @jdkVersion: 1.8
 * @author: fhx
 * @date: 2024/7/2 9:53
 */
@Slf4j
@RestController
@RequestMapping(ApiConstant.API_CABINET)
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ServiceShutdownAndReStartController {


    @PostMapping("/shutdown")
    public ResponseEntity<String> shutdown() throws IOException {

        String bat = System.getProperty("user.dir") + "//" + "stop.bat";
        log.info("====== 关闭服务请求 ====== {}", bat);
        new Thread(() -> {
            try {
                // 执行关闭脚本（需根据操作系统编写脚本）
                Runtime.getRuntime().exec(bat);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();

        return ResponseEntity.ok("Service is shutting down...");
    }


    @PostMapping("/restart")
    public ResponseEntity<String> restart() {
        String bat = System.getProperty("user.dir") + "//" + "restart.bat";
        log.info("====== 重启 ====== {}", bat);
        new Thread(() -> {
            try {
                // 执行重启脚本（需根据操作系统编写脚本）
                Runtime.getRuntime().exec(bat);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();

        return ResponseEntity.ok("Service will restart shortly...");
    }
}
